<?php

namespace App\Exceptions;

use App\Model\ApiRes;
use App\Model\CodeError;
use Exception;
use Illuminate\Http\JsonResponse;

class BusinessException extends Exception
{
    public array $codeError;

    public function __construct(array $codeError, $info = '')
    {
        $this->codeError = $codeError;
        if (!empty($info)) {
            $this->codeError[1] = $this->codeError[1] . "(" . $info . ")";
        }
        parent::__construct($info ?: $codeError[1], $codeError[2]);
    }

    public function response(): JsonResponse
    {
        $code = $this->codeError[0];
        if ($code == CodeError::OK[0]) {
            return ApiRes::create()->success([])->jsonResponse();
        } else {
            return ApiRes::create()->withCode($this->codeError, '')->jsonResponse();
        }
    }


}
